<?php

namespace app\common\model;

use app\common\library\HttpCurl;
use Qiniu\Auth;
use Qiniu\Processing\PersistentFop;
use think\Model;
use function Qiniu\base64_urlSafeEncode;


class Video extends Model
{

    

    

    // 表名
    protected $name = 'video';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'st_switch_text',
        'is_transfer_text',
        'times_format'
    ];
    

    
    public function getStSwitchList()
    {
        return ['0' => __('St_switch 0'), '1' => __('St_switch 1')];
    }

    public function getIsTransferList()
    {
        return ['0' => __('Is_transfer 0'), '1' => __('Is_transfer 1')];
    }


    public function getStSwitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['st_switch']) ? $data['st_switch'] : '');
        $list = $this->getStSwitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsTransferTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_transfer']) ? $data['is_transfer'] : '');
        $list = $this->getIsTransferList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected static function init()
    {
        self::afterWrite(function ($data) {
            self::initVideo($data['id']);
        });
//        setLog(__METHOD__,json_encode($data,JSON_UNESCAPED_UNICODE));
//

    }
    public static function initVideo($id)
    {
        $video = self::find($id);
        if(is_bool(strrpos($video,"http"))){
            $url=cdnurl($video['video_file']);
        }else{
            $url=$video['video_file'];
        }
        $url=str_replace(["https:","http:","//:cdn"],["","","//cdn"],$url);
        setLog(__METHOD__,$url);
        $result=HttpCurl::get("http:".$url."?avinfo");
        setLog(__METHOD__,$result);
        $result=json_decode($result,true);
        if(!isset($result['format'])) return false;
        $times=ceil($result['format']['duration']);
        $video->times=$times;
        $video->video_key=str_replace("//cdn.yingcaishenzhou.com/","",$video->video_file);
        $video->video_file=$url;
        if($video->weigh==0){
            $video->weigh=$video->id;
        }
        require_once EXTEND_PATH . 'qiniu/autoload.php';
        $auth = new Auth("RYRKa7TuZhn6AjhhaA_3f7N_GRLNNxrlX-oWS2WH", "7C_1MSoSQZMmkb2yXXrIHUDpuEYxmDAoJRfUI_vo");
        $config=new Config();
        $pfop=new PersistentFop($auth, $config);
        $bucket="guopin-pc";
        $videoKey=urldecode($video->video_key);
        $newKey="hls/".md5($videoKey).".m3u8";
        $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264|saveas/'.base64_urlSafeEncode($bucket.":".$newKey);
        list($id,$err)=$pfop->execute($bucket,$videoKey ,$fops,"20190709");
        $video->video_key=$videoKey;
        $video->safe_link="//cdn.yingcaishenzhou.com/".$newKey;
        $video->follow_id=$id;
        $video->save();
        return true;
    }

    public function getTimesFormatAttr($value, $data)
    {
        if(isset($data['times'])){
            return getVideoTime($data['times']);
        }else{
            return "";
        }
    }




}
